<template>
  <div class="user_box">
    <!-- 顶部搜索 -->
    <el-form
      ref="elForm"
      :inline="true"
      :model="formInline"
      class="demo-form-inline"
    >
      <div class="form_item">
        <span>标签名称</span
        ><el-form-item>
          <el-input v-model="formInline.tags" placeholder="请输入" @change="onSerch"></el-input>
        </el-form-item>
      </div>
      <div class="form_item">
        <span>城市</span>
        <el-select
          v-model="formInline.province"
          clearable
          placeholder="请选择"
          @change="getAreas"
        >
          <el-option
            v-for="item in datas"
            :key="item.value"
            :label="item.city"
            :value="item.city"
          >
          </el-option>
        </el-select>
      </div>
      <div class="form_item">
        <span>地区</span>
        <el-select v-model="formInline.city" clearable placeholder="请选择">
          <el-option
            v-for="item in areas"
            :key="item"
            :label="item"
            :value="item"
          >
          </el-option>
        </el-select>
      </div>
      <div class="form_item">
        <span>企业简称</span
        ><el-form-item>
          <el-input v-model="formInline.shortName" @change="onSerch"></el-input>
        </el-form-item>
      </div>
      <div class="form_item">
        <span>状态</span>
        <el-select v-model="formInline.state" clearable placeholder="请选择">
          <el-option
            v-for="item in statues"
            :key="item.value"
            :label="item.label"
            :value="item.value"
            @change="onSerch"
          >
          </el-option>
        </el-select>
      </div>
      <div class="form_item">
        <el-form-item>
          <el-button @click="inputClick">清空</el-button>
          <el-button type="primary" @click="onSerch">搜索</el-button>
        </el-form-item>
      </div>

      <!-- 新增user -->
      <el-button
        class="addBtn"
        type="primary"
        icon="el-icon-edit"
        @click="dialogBtn('create')"
        >新增用户</el-button
      >
    </el-form>

    <!-- 中间分隔 -->
    <div class="m_alert">
      <i class="el-icon-info"></i>
      <div class="m_text">
        共<span>{{ total }}</span
        >条数据
      </div>
    </div>

    <!-- 新增的表单 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible">
      <el-form :model="form" :rules="rules" label-width="100px">
        <el-form-item
          label="企业名称"
          :label-width="formLabelWidth"
          prop="shortName"
        >
          <el-input v-model="form.shortName"></el-input>
        </el-form-item>
        <el-form-item
          label="所属公司"
          :label-width="formLabelWidth"
          prop="company"
        >
          <el-input v-model="form.company"></el-input>
          <p>https://www.tianyancha.com （在此可查询所属公司全称及简称）</p>
        </el-form-item>
        <el-form-item
          label="城市地区"
          :label-width="formLabelWidth"
          prop="city"
        >
          <el-select
            v-model="form.province"
            clearable
            placeholder="请选择"
            @change="getAddAreas"
          >
            <el-option
              v-for="item in datas"
              :key="item.value"
              :label="item.city"
              :value="item.city"
            >
            </el-option>
          </el-select>
          <el-select v-model="form.city" clearable placeholder="请选择">
            <el-option
              v-for="item in areas"
              :key="item"
              :label="item"
              :value="item"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="方向（企业标签）"
          :label-width="formLabelWidth"
          prop="tags"
        >
          <el-input v-model="form.tags"></el-input>
        </el-form-item>

        <el-form-item label="备注" prop="remarks">
          <el-input type="textarea" v-model="form.remarks"></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelBtn">取 消</el-button>
        <el-button type="primary" @click="addForm">确 定</el-button>
      </div>
    </el-dialog>
    <!-- table表单 -->
    <template>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="shortName" label="序号" width="104">
        </el-table-column>
        <el-table-column prop="number" label="企业编号" width="104">
        </el-table-column>
        <el-table-column prop="shortName" label="企业简称" width="104">
        </el-table-column>
        <el-table-column prop="tags" label="标签" width="104">
        </el-table-column>
        <el-table-column prop="username" label="创建者" width="104">
        </el-table-column>
        <el-table-column prop="addDate" label="创建日期" width="104">
        </el-table-column>
        <el-table-column prop="remarks" label="备注" width="104">
        </el-table-column>
        <el-table-column prop="state" label="状态" width="104">
          <template #default="{ row }">
            <span>{{ row.state ? '已启用' : '已禁用' }}</span>
          </template>
        </el-table-column>

        <!--右侧标签 默认插槽 -->
        <el-table-column label="操作" width="260">
          <template #default="{ row }">
            <!-- <div class="actions">
              <i
                class="el-icon-edit el-icon"
                @click="editBtn('edit', row.id)"
              ></i>
              <i class="el-icon-check el-icon" @click="statueBtn(row.id)"></i>
              <i class="el-icon-delete el-icon" @click="del(row.id)"></i>
            </div> -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              circle
              class="edit1"
              @click="editBtn('edit', row.id)"
            ></el-button>
            <el-button
              v-if="row.state"
              type="primary"
              icon="el-icon-close"
              circle
              class="edit3"
              @click="statueBtn(row)"
            ></el-button>
            <el-button
              v-else
              type="primary"
              icon="el-icon-check"
              circle
              class="edit4"
              @click="statueBtn(row)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              class="edit2"
              @click="del(row.id)"
            ></el-button>
            <!-- <el-button type="text" @click="open">点击打开 Message Box</el-button> -->
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页导航 -->
      <div class="block">
        <el-pagination
          background
          :current-page="1"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="100"
          :total="total"
          layout=" prev, pager, next, sizes, jumper"
        >
        </el-pagination>
      </div>
    </template>
  </div>
</template>

<script>
import { datas } from '@/api/hmmm/citys.js'
import {
  list,
  add,
  remove,
  update,
  detail
  // disabled
} from '@/api/hmmm/companys.js'
import { parseTime } from '@/utils/index.js'

export default {
  data () {
    return {
      statue: 1,
      datas: '',
      citys: [],
      areas: '',
      statues: [
        {
          value: 1,
          label: '启用'
        },
        {
          value: 0,
          label: '禁用'
        }
      ],
      value: '',

      id: null,
      total: 0,
      dialogType: 'create',
      rules: {
        shortName: [
          { required: true, message: '请输入企业名称', trigger: 'blur' }
        ],
        city: [{ required: true, message: '请选择城市', trigger: 'blur' }],
        company: [
          { required: true, message: '请输入所属公司', trigger: 'blur' }
        ],
        tags: [{ required: true, message: '请输入标签', trigger: 'blur' }],
        remarks: [{ required: true, message: '请输入', trigger: 'blur' }]
      },

      dialogTableVisible: false,
      dialogFormVisible: false,
      form: {
        isFamous: true,
        shortName: '',
        company: '',
        province: '',
        city: '',
        tags: '',
        remarks: ''
      },
      formLabelWidth: '100px',

      data: {
        page: 1,
        pagesize: 10,
        tags: '',
        province: '',
        city: '',
        shortName: '',
        state: null
      },
      formInline: {
        tags: '',
        province: '',
        city: '',
        shortName: '',
        state: ''
      },
      tableData: []
    }
  },

  created () {
    this.datas = datas
    this.renderList()
  },

  methods: {
    // 提示对话框
    open () {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    openState () {
      this.$confirm('状态已改变, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$message({
            type: 'success',
            message: '状态改变成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '状态改变失败'
          })
        })
    },

    async statueBtn (row) {
      this.openState()
      row.state = !row.state
    },
    // 搜索表单的城市
    getAreas () {
      // console.log(this.formInline.province)
      const data = this.datas.filter(item => {
        return item.city === this.formInline.province
      })
      this.areas = data[0].area
      this.formInline.city = this.areas
    },
    // 新增表单的城市
    getAddAreas () {
      // console.log(this.formInline.province)
      const data = this.datas.filter(item => {
        return item.city === this.form.province
      })
      this.areas = data[0].area
      this.form.city = this.areas
    },
    // dialog
    dialogBtn (type) {
      this.dialogType = type
      // if (this.dialogType === 'create') {
      this.dialogFormVisible = true
    },
    cancelBtn () {
      this.dialogType = 'create'
      this.clearAddform()
      this.dialogFormVisible = false
    },

    clearAddform () {
      this.form.tags = ''
      this.form.province = ''
      this.form.city = ''
      this.form.shortName = ''
      this.form.state = ''
      this.form.company = ''
      this.form.remarks = ''
    },
    // 表单点击确认
    async addForm () {
      try {
        if (this.dialogType === 'create') {
          // this.formInline.province = this.form.province
          // this.formInline.city = this.form.city
          console.log(this.form)
          const data = await add(this.form)
          console.log(data)
        }
        if (this.dialogType === 'edit') {
          await update({ id: this.id, ...this.form })
          this.renderList()
          // console.log(res)
        }
        this.clearAddform()
        this.dialogFormVisible = false
        this.$message.success(
          this.dialogType === 'create' ? '添加成功' : '编辑成功'
        )
        this.renderList()
      } catch (error) {
        this.$message.error('添加失败')
      }
    },

    // 渲染页面
    async renderList () {
      const { data } = await list(this.data)
      // console.log(data.items)
      this.list = data.items.filter(item => {
        item.addDate = parseTime(item.addDate)
        return true
      })
      this.tableData = data.items
      this.total = data.counts
      // this.form.state = data.items.state
    },

    handleSizeChange (val) {
      this.pagesize = val
      // console.log(`每页 ${val} 条`)
    },
    handleCurrentChange (val) {
      this.page = val
      // console.log(`当前页: ${val}`)
    },

    // 修改按钮
    async editBtn (type, id) {
      this.id = id
      this.dialogType = type
      const { data } = await detail(id)
      // console.log(data)
      this.form.number = data.number
      this.form.id = data.id
      this.form.tags = data.tags
      this.form.province = data.province
      this.form.city = data.city
      this.form.shortName = data.shortName
      this.form.company = data.company
      this.form.remarks = data.remarks
      this.dialogFormVisible = true
    },

    // 删除
    async del (id) {
      try {
        await remove(id)
        this.$message.success('删除成功')
        this.renderList()
        // console.log(data)
      } catch (error) {
        this.$message.error('删除失败')
      }
    },

    // 清空搜索框
    inputClick () {
      this.formInline.tags = ''
      this.formInline.province = ''
      this.formInline.city = ''
      this.formInline.shortName = ''
      this.formInline.state = ''
      this.data.tags = ''
      this.data.province = ''
      this.data.city = ''
      this.data.shortName = ''
      this.data.state = null
      this.renderList()
    },
    // 搜索
    onSerch () {
      this.data.tags = this.formInline.tags
      this.data.province = this.formInline.province
      this.data.city = this.formInline.city
      this.data.shortName = this.formInline.shortName
      this.data.state = this.formInline.state
      this.renderList()
    }
  },
  computed: {
    dialogTitle () {
      if (this.dialogType === 'create') return '添加用户'
      if (this.dialogType === 'statue') return '修改状态'
      if (this.dialogType === 'edit') return '修改用户'
      else {
        return '添加用户'
      }
    }
  }
}
</script>

<style scoped lang="less">
.user_box {
  width: 1178px;
  // height: 417.5px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  border: 1px solid #ebeef5;
  background-color: #fff;
  color: #303133;
  transition: 0.3s;
  border-radius: 4px;
  overflow: hidden;
  margin: 20px;
  padding: 20px;

  .demo-form-inline {
    .form_item {
      width: 25%;
      height: 50px;
      float: left;
      box-sizing: border-box;
    }
    span {
      line-height: 32px;
      text-align: right;
      vertical-align: middle;
      float: left;
      font-size: 14px;
      color: #2a2b2b;
      box-sizing: border-box;
      padding: 0px 12px 0px 0px;
      padding-right: 12px;
      // width: 80px;
      // height: 32px;
      // padding: 12px;
    }
  }
}
.addBtn {
  float: right;
  margin-left: 10px;
  color: #fff;
  background-color: #67c23a;
  border-color: #67c23a;
  border: 1px solid #dcdfe6;
  font-weight: 500;
}
.m_alert {
  display: flex;
  background-color: #f4f4f5;
  color: #909399;
  width: 100%;
  padding: 8px 16px;
  margin: 0;
  border-radius: 4px;
  color: #909399;
  i {
    display: inline-block;
    font-size: 16px;
    width: 16px;
    line-height: 1;
    margin-right: 5px;
  }
  .m-text {
    font-size: 13px;
    line-height: 18px;
  }
}
.el-pagination {
  margin-top: 20px;
  text-align: right;
}

.actions {
  width: 60px;
  height: 36px;
  margin-right: 5px;
  // padding-left: 5px;
  // padding-right: 5px;
  .el-icon {
    width: 14px;
    height: 14px;
    font-size: 14px;
    padding: 10px;

    line-height: 1;
    border-radius: 50%;
    display: inline-block;
    line-height: 1;
    white-space: nowrap;
    cursor: pointer;
    border: 1px solid #dcdfe6;
    transition: 0.1s;
    font-weight: 500;
  }
  .el-icon-edit {
    margin: 0 5px;
    margin-top: 6px;
    color: #409eff;
    background: #ecf5ff;
    border-color: #b3d8ff;
  }
  .el-icon-delete {
    margin-right: 5px;
    color: #f56c6c;
    background: #fef0f0;
    border-color: #fbc4c4;
  }
}
.edit1 {
  color: #409eff;
  background: #ecf5ff;
  border-color: #b3d8ff;
  &:hover {
    background-color: #409eff;
    color: #fff;
  }
}
.edit2 {
  color: #f56c6c;
  background: #fef0f0;
  border-color: #fbc4c4;
  &:hover {
    background-color: #f56c6c;
    color: #fff;
  }
}
.edit3 {
  color: #e6a23c;
  background: #fdf6ec;
  border-color: #f5dab1;
  &:hover {
    background-color: #f56c6c;
    color: #fff;
  }
}
.edit4 {
  color: #67c23a;
  background: #f0f9eb;
  border-color: #c2e7b0;
  &:hover {
    background-color: #f56c6c;
    color: #fff;
  }
}
</style>
